<template>
    <div class="an-page-children an-address">
        <van-nav-bar
            :title="title"
            left-text=""
            left-arrow
            @click-left="clickBack"
            />

        <van-address-edit
            ref="addressEdit"
            :area-list="areaList"
            show-set-default
            show-search-result
            :address-info="addressInfo"
            :search-result="searchResult"
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            :is-saving="loading"
            @save="onSave"
            />
    </div>
</template>

<script>
import areaList from "@/api/area.js";
export default {
  props:{
    prom:{ // 地址数据
      type: String,
      default: ''
    }
  },
  data() {
    return{
      title:'新增收货地址',
      areaList,
      searchResult: [],
      address:{}, // 地址数据
      addressInfo:{},
      loading: false
    };
  },
  mounted() {
    this.anLoad();
  },
  methods:{
    anLoad() {
      if(this.prom) {
        this.title = '编辑收货地址';
        let nd =JSON.parse(decodeURI(this.prom));
        let address = nd.address.split('/');
        this.addressInfo = {
          id: nd.id,
          name: nd.name,
          tel: nd.tel,
          province:'', // 省份
          city:'', // 城市
          county:'', // 区县
          addressDetail: address[address.length-1],
          areaCode: nd.areaCode+'', // 地区编码，通过省市区选择获取（必填）
          isDefault: nd.isDefault
        };
      }
    },
    onSave(form) {
      let addressDetail = form.province + '/' + form.city + '/' + form.county + '/' + form.addressDetail;
      let nd = {
        id: form.id,
        consignee: form.name,
        address: addressDetail,
        tel: form.tel,
        isDefault: form.isDefault?1:0,
        areaCode: form.areaCode,
        userID: this.$store.state.userInfo.id
      };
      this.loading = true;
      this.$api.userAddressAdd(nd).then(() => {
        this.loading = false;
        this.$toast.success('保存成功');
        this.clickBack();
      });
    },

    clickBack() {
      this.$router.go(-1);
    }
  }
};
</script>